﻿#region Using Statements

using System;
using System.Text;
using System.Collections.Generic;
using System.Linq;
using System.IO;
using System.Threading;
using System.Xml;
using System.Xml.Serialization;
using System.Runtime.Serialization;
using System.Security.Permissions;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Media;
using Microsoft.Xna.Framework.Net;
using Microsoft.Xna.Framework.Storage;

#endregion


namespace Freeform
{
    /// <summary>
    /// Entities are renderable components that make up a scene in a world.
    /// </summary>
    public class Entity : Component
    {

        #region Members

        /// <summary>
        /// Gets the world that this entity renders to.
        /// </summary>
        public virtual World World { get; private set; }


        /// <summary>
        /// Gets or sets the position of the entity.
        /// </summary>
        public virtual SpatialProperties SpatialProperties { get; private set; }


        /// <summary>
        /// Gets or sets how long this entity has been active for (in milliseconds.)
        /// </summary>
        public virtual UInt32 Age { get; set; }


        /// <summary>
        /// Gets the actor for this entity.
        /// </summary>
        public virtual Actor Actor { get; set; }


        /// <summary>
        /// Gets the collision properties for this entity.
        /// </summary>
        public virtual CollisionProperties CollisionProperties { get; internal set; }

        #endregion


        #region Init

        /// <summary>
        /// Creates a new entity and automatically links it to the given world.
        /// </summary>
        /// <param name="world">The world that this entity belongs to.</param>
        public Entity(World world) 
            : base(world.ComponentList)
        {
            World = world;
            IsEnabled = true;
            IsLoaded = true;
            SpatialProperties = new SpatialProperties();
            Age = 0;
            new CollisionProperties(this);
        }

        #endregion


        #region Update

        /// <summary>
        /// Allows the entity to update itself.
        /// </summary>
        /// <param name="time">Provides a snapshot of timing values.</param>
        public override void Update(GameTime time)
        {
            Age += (UInt32)time.ElapsedGameTime.Milliseconds;

            if (SpatialProperties.MotionController != null)
                SpatialProperties.MotionController.Update(time);

            base.Update(time);
        }

        #endregion


        #region Draw

        /// <summary>
        /// Allows the entity to draw itself.
        /// </summary>
        /// <param name="time">Provides a snapshot of timing values.</param>
        /// <param name="batch">The spritebatch to render to.</param>
        public virtual void Draw(GameTime time, Viewport2D viewport)
        {
            if (Actor != null && Actor.Visible)
                Actor.Draw(viewport, time);
        }

        #endregion


        #region Dispose

        /// <summary>
        /// Disposes the entity and removes it from the world.
        /// </summary>
        protected override void OnDispose()
        {
            base.OnDispose();

            // Remove reference from world's entity list.
            World.ComponentList.Library.Remove(ComponentID);
        }

        #endregion


        #region Utility

        /// <summary>
        /// Gets the location as a factor of game-screen resolution of this entity.
        /// </summary>
        /// <param name="viewport">The viewport with which we wish to look through.</param>
        /// <returns>The screen coordinates as a factor of game-screen resolution.</returns>
        public virtual Vector2 GetScreenLocation(Viewport2D viewport)
        {
            return viewport.SpatialProperties.Position + (SpatialProperties.Position - viewport.Camera.SpatialProperties.Position) / viewport.Camera.SpatialProperties.Size * viewport.SpatialProperties.Size;
        }


        
        public virtual Boolean IsMouseOver(Vector2 sizeScaling)
        {
            for (int i = 0; i < World.Viewports.Count; i++)
            {
                // Check if the mouse is over this viewport.
                if (World.Viewports[i].IsMouseOver)
                {
                    // Find the coordinates of the mouse in world units.
                    Vector2 MouseLoc = World.Viewports[i].MouseLocation_World;

                    // Check if the mouse is over our entity.
                    if (MouseLoc.X > SpatialProperties.Position.X - SpatialProperties.Size.X * sizeScaling.X / 2 &&
                        MouseLoc.Y > SpatialProperties.Position.Y - SpatialProperties.Size.Y * sizeScaling.Y / 2 &&
                        MouseLoc.X < SpatialProperties.Position.X + SpatialProperties.Size.X * sizeScaling.X / 2 &&
                        MouseLoc.Y < SpatialProperties.Position.Y + SpatialProperties.Size.Y * sizeScaling.Y / 2)
                        return true;
                }
            }
            return false;
        }


        
        public virtual Boolean IsMouseOver()
        {
            for (int i = 0; i < World.Viewports.Count; i++)
            {
                // Check if the mouse is over this viewport.
                if (World.Viewports[i].IsMouseOver)
                {
                    // Find the coordinates of the mouse in world units.
                    Vector2 MouseLoc = World.Viewports[i].MouseLocation_World;

                    // Check if the mouse is over our entity.
                    if (MouseLoc.X > SpatialProperties.Position.X - SpatialProperties.Size.X / 2 &&
                        MouseLoc.Y > SpatialProperties.Position.Y - SpatialProperties.Size.Y / 2 &&
                        MouseLoc.X < SpatialProperties.Position.X + SpatialProperties.Size.X / 2 &&
                        MouseLoc.Y < SpatialProperties.Position.Y + SpatialProperties.Size.Y / 2)
                        return true;
                }
            }
            return false;
        }

        #endregion

    }
}